package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Zwnan
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    private DtsRoleMapper dtsRoleMapper;

    @Override
    public Set<String> findByIds(Integer[] roleIds) {
        List<DtsRole> dtsRoleList = dtsRoleMapper.selectBatchIds(Arrays.asList(roleIds));
        //Stream流   可读性极差
        Set<String> roles = dtsRoleList.stream().map(DtsRole::getName).collect(Collectors.toSet());
        return roles;
    }

    @Override
    public List<CatVo> findAll() {

        List<DtsRole> dtsRoleList = dtsRoleMapper.selectList(new QueryWrapper<DtsRole>().select("id", "name"));
        List<CatVo> catVoList = dtsRoleList.stream().map(dtsRole -> {
            CatVo vo = new CatVo();
            vo.setValue(dtsRole.getId());
            vo.setLabel(dtsRole.getName());
            return vo;
        }).collect(Collectors.toList());

        return catVoList;
    }

    @Override
    public IPage<DtsRole> selectPage(Integer page, Integer limit, String sort, String order) {
        IPage<DtsRole> iPage = new Page<>(page,limit);
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper();
        queryWrapper.eq("deleted", false);
        return dtsRoleMapper.selectPage(iPage,queryWrapper);
    }

    @Override
    public void create(DtsRole dtsRole) {

        dtsRoleMapper.insert(dtsRole);

    }

    @Override
    public void update(DtsRole dtsRole) {
        dtsRole.setUpdateTime(new Date());
        dtsRoleMapper.updateById(dtsRole);
    }

    @Override
    public void delete(DtsRole dtsRole) {
        dtsRole.setDeleted(true);
        dtsRoleMapper.updateById(dtsRole);
    }

}
